package com.tuojiahr.action;

import java.io.IOException;
import java.util.List;

import com.tuojiahr.common.BaseActionCommon;
import com.tuojiahr.domain.TuoJiaHrColumn;
import com.tuojiahr.domain.TuoJiaHrEmployee;
import com.tuojiahr.domain.TuoJiaHrOrdinaryColumn;
import com.tuojiahr.domain.TuoJiaHrQQContact;
import com.tuojiahr.domain.TuoJiaHrSpecialColumn;
import com.tuojiahr.global.Constants;
import com.tuojiahr.pagemodel.PageModel;
import com.tuojiahr.service.ColumnService;
import com.tuojiahr.service.EmployeeService;
import com.tuojiahr.service.OrdinaryColumnService;
import com.tuojiahr.service.QQService;
import com.tuojiahr.service.SpecialColumnService;
import com.tuojiahr.service.SpecialEntryService;
import com.tuojiahr.tools.StringUtil;

/**
 * 说明：
 *
 * @author wang.haibo
 * @date 2012-11-11 下午10:52:46
 */

public class WebAction extends BaseActionCommon {

	private static final long serialVersionUID = 1L;

	private ColumnService columnService;
	private EmployeeService employeeService;
	private SpecialEntryService specialEntryService;
	private OrdinaryColumnService ordinaryColumnService;
	private SpecialColumnService specialColumnService;
	private QQService qqService;
	
	private TuoJiaHrEmployee emp;	//在线报名人员
	
	private String url = "";
	private String message = "出错啦！！！";
	
	/**
	 * 获得头部导航
	 * @return
	 */
	public String header() {
		List<TuoJiaHrColumn> columnList = columnService.getColoumns();
		request.setAttribute("columnList", columnList);
		return "header";
	}
	
	/**
	 * 获得右侧数据
	 * 如果长度超过两个则只显示前面的两个
	 * @return
	 */
	public String right() {
		List<TuoJiaHrOrdinaryColumn> ordinaryHomeShowList = columnService.getOrdinaryColumnIsHomeShow();
		if(null != ordinaryHomeShowList && 2 < ordinaryHomeShowList.size()) {
			ordinaryHomeShowList = ordinaryHomeShowList.subList(0, 2);
		}
		request.setAttribute("ordinaryHomeShowList", ordinaryHomeShowList);
		return "right";
	}

	/**
	 * 首页中央显示区域
	 * @return
	 */
	public String center() {
		PageModel pageModel = ordinaryColumnService.getOrdinaryColumn(null);
		//显示普通栏目的第一条
		if(null != pageModel && 0 < pageModel.getTotal()) {
			TuoJiaHrOrdinaryColumn ordinaryColumn = (TuoJiaHrOrdinaryColumn) pageModel.getList().get(0);
			request.setAttribute("ordinaryColumn", ordinaryColumn);
		}
		
		//显示专栏的前两条
		PageModel specialPageModel = specialColumnService.getSpecialColumn(null);
		if(null != specialPageModel && 3 < specialPageModel.getTotal()) {
			specialPageModel.setList(specialPageModel.getList().subList(0, 2));
		}
		request.setAttribute("specialList", specialPageModel.getList());
		return "center";
	}
	
	/**
	 * 打开在线报名
	 * @return
	 */
	public String openSignUp() {
		return "signup_view";
	}
	
	/**
	 * 验证报名的手机号是否已经存在
	 * y已报名，n未报名
	 * @throws IOException 
	 */
	public void checkMobile() throws IOException {
		String result = "y";
		if(null != emp && StringUtil.isNotBlank(emp.getMobile())) {
			try {
				result = employeeService.checkMobile(emp.getMobile());
			} catch (Exception e) {
				Constants.ExceptionLog.error("##WebAction-----checkMobile方法查询失败：", e);
			}
		}
		response.getWriter().print(result);
		response.getWriter().close();
	}
	
	/**
	 * 在线报名
	 * @return
	 */
	public String signUp() {
		if(null != emp) {
			employeeService.addEmployee(emp);
		}
		return "sign_up_succ";
	}
	
	/**
	 * 根据选择的导航，加载不同的内容
	 * @return
	 */
	public String showChoseView() {
		String id = request.getParameter("id");
		String type = "ordinary";
		if(StringUtil.isNotBlank(id)) {
			TuoJiaHrColumn column = columnService.getColumnById(Long.valueOf(id));
			if(column instanceof TuoJiaHrOrdinaryColumn) {		//普通栏目
				TuoJiaHrOrdinaryColumn ordinaryColumn = (TuoJiaHrOrdinaryColumn) column;
				request.setAttribute("ordinaryColumn", ordinaryColumn);
			} else if(column instanceof TuoJiaHrSpecialColumn) {	//特殊栏目
				//加载特殊栏目下的实体
				PageModel pageModel = specialEntryService.getEntryList(null, Long.valueOf(id));
				url = "web_showChoseView";
				request.setAttribute("pageModel", pageModel);
				request.setAttribute("id", id);
				request.setAttribute("specialName", column.getName());
				type = "special";
			}
			request.setAttribute("type", type);
			return "show_chose_view";
		}
		return "";
	}
	
	/**
	 * 加载qq
	 * @return
	 */
	public String getQQ() {
		List<TuoJiaHrQQContact> qqList = qqService.getQqList();
		request.setAttribute("qqList", qqList);
		return "qq_view";
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void setColumnService(ColumnService columnService) {
		this.columnService = columnService;
	}

	public TuoJiaHrEmployee getEmp() {
		return emp;
	}

	public void setEmp(TuoJiaHrEmployee emp) {
		this.emp = emp;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	public void setSpecialEntryService(SpecialEntryService specialEntryService) {
		this.specialEntryService = specialEntryService;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setOrdinaryColumnService(OrdinaryColumnService ordinaryColumnService) {
		this.ordinaryColumnService = ordinaryColumnService;
	}

	public void setSpecialColumnService(SpecialColumnService specialColumnService) {
		this.specialColumnService = specialColumnService;
	}

	public void setQqService(QQService qqService) {
		this.qqService = qqService;
	}
	
}
